package com.amit.entities;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: AD
 * Date: 05/01/13
 * Time: 11:24
 */

@SuppressWarnings("UnusedDeclaration")
@Entity
@Table(name = "LECTURE")
public class LectureEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "LECTURE_ID")
    private Long id;

    @Column(name = "STUDENTS")
    @OneToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "LECTURES_STUDENTS")
    private Set<StudentEntity> students;

    @Column(name = "NAME", nullable = false)
    private String name;

    public LectureEntity() {
    }

    public LectureEntity(Long id, Set<StudentEntity> students, String name) {
        this.id = id;
        this.students = students;
        this.name = name;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Set<StudentEntity> getStudents() {
        return students;
    }

    public void setStudents(Set<StudentEntity> students) {
        this.students = students;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void addStudent(StudentEntity student) {
        students.add(student);
    }

    public void removeStudent(StudentEntity student) {
        students.remove(student);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        LectureEntity lecture = (LectureEntity) o;

        return !(id != null ? !id.equals(lecture.id) : lecture.id != null) &&
                !(name != null ? !name.equals(lecture.name) : lecture.name != null) &&
                !(students != null ? !students.equals(lecture.students) : lecture.students != null);
    }

    @Override
    public int hashCode() {
        int result = id != null ? id.hashCode() : 0;
        result = 31 * result + (students != null ? students.hashCode() : 0);
        result = 31 * result + (name != null ? name.hashCode() : 0);
        return result;
    }
}
